package com.hsq.ndmall.product.service.impl;


import com.hsq.ndmall.product.entity.GoodsEntity;
import com.hsq.ndmall.product.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hsq.ndmall.product.dao.BrandDao;
import com.hsq.ndmall.product.entity.BrandEntity;
import com.hsq.ndmall.product.service.BrandService;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.Query;

@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    GoodsService goodsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                getQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    private QueryWrapper getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        if (params.containsKey("goods_id")) {
            // 先根据商品ID查询商品
            GoodsEntity goods = goodsService.getById(Integer.parseInt((String) params.get("goods_id")));
            // 根据商品信息的生产厂家Id查询厂家信息
            queryWrapper.lambda().eq(BrandEntity::getId, goods.getBrandId()).eq(BrandEntity::getDeleted,0);
        }

        return queryWrapper;
    }
}
